﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.XPath;

namespace JanusTest
{
    class JanusCheck
    {
        private static void start_post(string strPage, string strBuffer)
        {
            //Our postvars
            byte[] buffer = Encoding.ASCII.GetBytes(strBuffer);
            //Initialisation
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(strPage);
            //Our method is post, otherwise the buffer (postvars) would be useless
            WebReq.Method = "POST";
            //We use form contentType, for the postvars.
            WebReq.ContentType = "application/x-www-form-urlencoded";
            //The length of the buffer (postvars) is used as contentlength.
            WebReq.ContentLength = buffer.Length;
            //We open a stream for writing the postvars
            Stream PostData = WebReq.GetRequestStream();
            //Now we write, and afterwards, we close. Closing is always important!
            PostData.Write(buffer, 0, buffer.Length);
            PostData.Close();
            //Get the response handle, we have no true response yet!
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            //Let's show some information about the response
            Console.WriteLine(WebResp.StatusCode);
            Console.WriteLine(WebResp.Server);

            //Now, we read the response (the string), and output it.
            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            Console.WriteLine(_Answer.ReadToEnd());
        }

        private static void start_get(string strPage, string strVars)
        {
            //Initialisation
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("{0}{1}", strPage, strVars));
            //This time, our method is GET.
            WebReq.Method = "GET";
            //From here on, it's all the same as above.
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            //Let's show some information about the response
            Console.WriteLine(WebResp.StatusCode);
            Console.WriteLine(WebResp.Server);

            //Now, we read the response (the string), and output it.
            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            Console.WriteLine(_Answer.ReadToEnd());
        }

        internal String getJanusRole()
        {
            FileStream fstream = new FileStream("XMLFile1.xml", FileMode.Open);
            byte[] barr = new byte[new FileInfo("XMLFile1.xml").Length];
            fstream.Read(barr, 0, barr.Length );
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://janus-uat.ny.jpmorgan.com:39443/jws/");
            webRequest.Method = "POST";
            Stream stream = webRequest.GetRequestStream();
            stream.Write(barr, 0, barr.Length);
            stream.Flush();
            stream.Close();
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            StreamReader responseStreamReader = new StreamReader(webResponse.GetResponseStream());
            return responseStreamReader.ReadToEnd();
        }
    }
}
